﻿using Consul;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartProduction.Domain;
using SmartProduction.ErrorCode;
using SmartProduction.Write.API.Application.Commands.BomCommand;

namespace SmartProduction.Write.API.Controllers
{
    /// <summary>
    /// BOM管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BomController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<BomController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public BomController(ILogger<BomController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">重置</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<APIResult<object>> CreateBom(BomCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">命令</param>
        /// <param name="cancellationToken">操作</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<APIResult<object>> Handle(BomPartsCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加工艺流程
        /// </summary>
        /// <param name="request">命令</param>
        /// <param name="cancellationToken">操作</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<APIResult<object>> AddProc(ProcessInfoCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改BOM
        /// </summary>
        /// <param name="request">命令</param>
        /// <param name="cancellationToken">操作</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<APIResult<object>> EditBOM(EditBomCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
